/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.misena.praxispraktik.recursos;

import com.misena.praxispraktik.entity.Empresas;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;

/**
 *
 * @author diana
 */
@Stateless
@Path("/empresa")
public class EmpresasRest {

    Empresas empresa;
    EntityManagerFactory emf = null;
    EntityManager em = null;

    @GET
    @Produces("application/json")
    public Empresas consultar(@QueryParam("id") Integer id) {
        //public String consultar(@QueryParam("id") Integer id){
        //public String consultar(){
        try {
            emf = obtenerEntityManagerFactory();
            em = obtenerEntityManager(emf);
            empresa = em.find(Empresas.class, id);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            em.close();
            emf.close();
        }
        System.out.println("Fin Consulta");

        System.out.println(empresa.getIdEmpresas() + " " + empresa.getNombre());

        return empresa;
    }

    @POST
    @Consumes("application/json")
    public Response guardar(Empresas emp) {
        empresa = emp;
        
        try {
            emf = obtenerEntityManagerFactory();
            em = obtenerEntityManager(emf);
            em.persist(emp);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            em.close();
            emf.close();
        }
        System.out.println("Fin de la inserción");

        return Response.ok().build();
    }

    /**
     *
     * @param a
     * @return
     */
    @PUT
    @Consumes("application/json")
    public Response actualizar(Empresas emp) {
        empresa = emp;
        //cliente = p;
        //return Response.ok(p).build();

        try {
            emf = obtenerEntityManagerFactory();
            em = obtenerEntityManager(emf);
            em.merge(emp);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            em.close();
            emf.close();
        }
        System.out.println("Fin de la actualización");

        return Response.ok().build();
    }

    @DELETE
    @Consumes("application/json")
    public Response Eliminar(@QueryParam("id") Integer id) {

        try {
            emf = obtenerEntityManagerFactory();
            em = obtenerEntityManager(emf);
            empresa = em.find(Empresas.class, id);

            if (!empresa.getIdEmpresas().equals(null)) {
                em.remove(empresa);
            } else {
                return Response.noContent().build();
            }



        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            em.close();
            emf.close();
        }
        System.out.println("Fin de la eliminación");
        return Response.ok().build();
    }

    public EntityManagerFactory obtenerEntityManagerFactory() {

        return Persistence.createEntityManagerFactory("PraxisPraktikPU");
    }

    public EntityManager obtenerEntityManager(EntityManagerFactory emf) {

        return emf.createEntityManager();
    }
}
